﻿package demo.util {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Rectangle;

	public class UniformScale {
		/**
		 * 等比缩放至指定宽度 返回缩放后的高度
		 *
		 * @param targetW
		 * @param targetH
		 * @param scaleW
		 *
		 * @return
		 */
		public static function scaleByWidth(targetW:int, targetH:int, scaleW:int):Number {
			return (targetH / targetW) * scaleW;
		}

		/**
		 * 等比缩放至指定高度 返回缩放后的宽度
		 *
		 * @param targetW
		 * @param targetH
		 * @param scaleH
		 * @return
		 *
		 */
		public static function scaleByHeight(targetW:int, targetH:int, scaleH:int):Number {
			return (targetW / targetH) * scaleH;
		}

		/**
		 * 等比缩放至指定容器大小 返回矩形 rect.width rect.heigth 为目标width height
		 *
		 * @param targetW
		 * @param targetH
		 * @param scaleW
		 * @param scaleH
		 *
		 * @return
		 */
		public static function scaleByBox(targetW:int, targetH:int, scaleW:int, scaleH:int):Rectangle {
			var scale:Number = Math.min(scaleW / targetW, scaleH / targetH);
			var rect:Rectangle = new Rectangle();
			rect.width = targetW * scale;
			rect.height = targetH * scale;
			return rect;
		}
	}
}

